<?php if(!defined('WDK_PATH')) exit('No direct script access allowed');
abstract class WdkController
{
	protected $tpl;
	protected $skin;
	protected $uri;
	protected $config;
	protected $layout;
	protected $session;
	protected $user_seq;
	
	function __construct()
	{
	}
	
	function __destruct()
	{
	}
	
	protected function _isLogin(){}
	protected function _loginPage(){}
	
	final protected function _loginCheck()
	{
		if($this->_isLogin() === false)
		{
			$this->_loginPage();
		}
		$this->user_seq = $this->session['user_seq'];
	}
	
	protected function _error($e)
	{
		echo $e->getMessage();
		exit;
	}
	
	protected function _view()
	{
		
	}
	
	protected function _redirect($path='')
	{
		header("Location: http://".$this->config['domain'].str_replace("//", "/", '/'.$path));
		exit;
	}
	protected function _layout($layout='')
	{
		$this->layout = $layout;
	}
	protected function _isPost()
	{
		return $_SERVER['REQUEST_METHOD'] == "POST";
	}

	final protected function _script($scripts = array())
	{
		array_unshift($scripts, '<script>');
		array_unshift($scripts, '<body>');
		array_unshift($scripts, '</head>');
		array_unshift($scripts, '<title>MSG</title>');
		array_unshift($scripts, '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
		array_unshift($scripts, '<head>');
		array_unshift($scripts, '<html>');
		array_push($scripts, '</script>');
		array_push($scripts, '</body>');
		array_push($scripts, '</html>');
		echo implode("\n", $scripts);
		exit;
	}
	
	protected function _json($data=array(), $return=false)
	{
		if($return == true)
		{
			return htmlspecialchars(json_encode($data), ENT_QUOTES);
		}
		echo json_encode($data);
		exit;
	}
	
	protected function _parsing(&$output)
	{
		while(true)
		{
			$has_plugin = $this->_parseInclude($output);
			$has_include = $this->_parsePlugin($output);
			if($has_plugin == false && $has_include == false)
			{
				return;
			}
		}
	}

	protected function _parseInclude(&$output)
	{
		static $icall = 0;
		$icall++;
		if($icall == 10)
		{
			exit('Include error');
		}
		if(preg_match_all("@{# include (.*) #}@siU", $output, $matches))
		{
			list($include, $file) = $matches;
			foreach($include as $i=>$ddd)
			{
				$tid = str_replace('.html','',$file[$i]);
				$this->tpl->define($tid, $file[$i]);
				$file[$i] = $this->tpl->fetch($tid);
			}
			$output = str_replace($include, $file, $output);
		}
		return strpos($output, "{# plugin ");
	}

	protected function _parsePlugin(&$output)
	{
		static $pcall = 0;
		$pcall++;
		if($pcall == 10)
		{
			exit('Plugin error');
		}
		if(preg_match_all("@{# plugin ([a-zA-Z0-9_-]*)(|\s[^\s]*) #}@siU", $output, $matches))
		{
			$texts = $matches[0];
			$plugins = $matches[1];
			$params = $matches[2];
			foreach($texts as $i=>$text)
			{
				$plugin = $plugins[$i];
				parse_str($params[$i], $param);
				$plg = Wdk::plugin($plugin, $param);
				$output = str_replace($text, $plg->exec(), $output);
				unset($text, $plugin, $param, $plg);
			}
			unset($texts, $plugins, $params);
		}
		return strpos($output, "{# include ");
	}
}
function post($name='', $default='')
{
	if(array_key_exists($name, $_POST))
	{
		return $_POST[$name];
	}
	return $default;
}
function get($name='', $default='')
{
	if(array_key_exists($name, $_GET))
	{
		return $_GET[$name];
	}
	return $default;	
}